package eyrolles;

import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletContext;

import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.action.ActionServlet;

public class EyrollesPlugin implements PlugIn {

    public static final String PROPERTIES = "PROPERTIES";
    private String filePath = null;

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String value) {
        filePath = value;
    }

    @Override
    public void init(ActionServlet servlet, ModuleConfig applicationConfig)
            throws javax.servlet.ServletException {

        System.out.println("---->Le plug-in demarre<----");
        Properties properties = new Properties();

        try {
            File file =
                 new File(getFilePath());

            FileInputStream fis =
                            new FileInputStream(file);

            properties.load(fis);

            ServletContext context = servlet.getServletContext();
            context.setAttribute(PROPERTIES, properties);
        }
        catch (FileNotFoundException fnfe) {

            throw new ServletException(fnfe.getMessage());
        }
        catch (IOException ioe) {

            throw new ServletException(ioe.getMessage());
        }
    }

    @Override
    public void destroy() {

        System.out.println("---->Le plug-in s'arrete<----");
    }
}
